package com.market.cash.ui.fragment

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.market.cash.R
import com.market.cash.databinding.CashPermissionPageBinding
import com.market.cash.pro.ProFragment
import com.market.cash.utils.CashRootDensityUtil
import com.market.cash.utils.adapter.CashRootRecyclerViewAdapter
import com.market.cash.utils.adapter.CashRootViewHolderHelper
import com.market.cash.utils.decoration.LinearSpacingItemDecoration
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/9/4 15:03
 * @Annotation：权限Page
 */
class PermissionFragment(): ProFragment() {
    lateinit var binding: CashPermissionPageBinding
    private var data: JSONObject? = null

    //权限描述列表
    private var permissionAdapter: ItemAdapter? = null

    constructor(data: JSONObject): this(){
        this.data = data
    }

    override fun getCashRootRootView(inflater: LayoutInflater): View {
        binding = CashPermissionPageBinding.inflate(inflater)
        return binding.root
    }

    override fun initCashRootView() {
        rootViewBinding.rootLayout.setBackgroundColor(Color.parseColor("#00000000"))
        initCashRootRecyclerView()
    }

    override fun initCashRootListener() {
    }

    override fun initCashRootData() {
        val desList = arrayListOf<JSONObject>()
        if (data != null){
            val desArray = data!!.optJSONArray("msgList")
            if (desArray != null && desArray.length() > 0){
                for (i in 0..<desArray.length()){
                    desList.add(desArray.optJSONObject(i))
                }
            }
        }
        permissionAdapter!!.data = desList
    }

    /* 初始化列表 */
    private fun initCashRootRecyclerView(){
//        binding.rvPermission.visibility = View.GONE
//        binding.rvPermission.setHasFixedSize(true)
//        binding.rvPermission.isNestedScrollingEnabled = false
//        binding.rvPermission.isFocusableInTouchMode = false //不需要焦点
        val layoutManager = LinearLayoutManager(context)
        binding.recyclerView.layoutManager = layoutManager
        val topDecoration = CashRootDensityUtil.dp2px(13f)
        binding.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0, 0, 0, topDecoration))
        permissionAdapter = ItemAdapter(binding.recyclerView)
        permissionAdapter!!.setOnItemChildClickListener { _, _, position ->
            val item = permissionAdapter!!.getItem(position)
        }
        binding.recyclerView.adapter = permissionAdapter
    }

    class ItemAdapter(recyclerView: RecyclerView?) : CashRootRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.cash_permission_item) {

        override fun setItemChildListener(helper: CashRootViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }

        override fun fillData(helper: CashRootViewHolderHelper, position: Int, model: JSONObject) {
            helper.setText(R.id.textTitle,model.optString("title"))
            helper.setText(R.id.textDes,model.optString("des"))
        }
    }
}